$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			table:{},
			form:{},
			queryData:{},
			groupAddIndex:'',
			saveUrl:'',
			groupAddId:'',
			groupId:$("#groupId").val(),
			isAdmin:$("#isAdmin").val(),
		},
		methods: {
			init:function () {
				var vm = this;
				layui.use(['table','form','layer'], 
					function(){
						  var table = layui.table;
						  var form = layui.form;
						  vm.table = table;
						  vm.form = form;
						  table.render({
								 id:"roleGroupDatagrid"
							    ,elem: '#datagrid'
							    ,height: 355
							    ,url: staticURL+'/basic/roleGroupAction/datagrid?groupId='+vm.groupId
							    ,page: true //开启分页
							    ,cols: [[ //表头
							      {type:'checkbox'}
							      ,{field: 'name', title: '角色名称',width:150,}
							      ,{field: 'description', title: '角色描述',width:150, }
							    ]]
							    ,response:{
						   		  msgName: 'msg' //状态信息的字段名称，默认：msg
						   		  ,countName: 'total' //数据总数的字段名称，默认：count
						   		  ,dataName: 'rows' //数据列表的字段名称，默认：data
						   		} 
							    ,request: {
								  pageName: 'page' //页码的参数名称，默认：page
								  ,limitName: 'rows' //每页数据量的参数名，默认：limit
								}  
						  });
					  
					}
				);
			},
			query:function () {
				this.table.reload('roleGroupDatagrid', {
					  where: this.queryData
				});
			},
			addRoleToGroup:function(){
				layer.open({
					  type: 2, 
					  title:"选择角色",
					  area: ['80%', '80%'],
					  icon:0,
					  content: staticURL+'/basic/roleInfoAction/goRoleInfo?dialogModel=true',
					  btn:['确认选择']
					  ,yes: function(index, layero){
						  //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
						  var iframeWin = window[layero.find('iframe')[0]['name']]; 
						  console.log(iframeWin);
						  var data = iframeWin.dialogChooseRole();
						  data.roleId = data.id;
						  data.groupId = app.groupId;
						  $.ajax({
								url : staticURL+"/basic/roleGroupAction/add",
								data : data,
								dataType : 'json',
								success : function(r) {
									layer.msg( r.msg);
									if (r.success) {
										app.query();
										layer.close(index);
									}
								}
							});
					  }
					});
			},
			deleteItem:function(){
				var rows = this.table.checkStatus('roleGroupDatagrid').data;
				var ids = "";
				if (rows.length == 1) {
					layer.confirm('您要删除当前所选项目？', {
						  btn: ['确定','取消'] //按钮
						}, function(){
							$.ajax({
								url : staticURL+'/basic/roleGroupAction/delete',
								data : JSON.stringify(rows[0]),
								contentType:"application/json",
								success : function(response) {
									app.query();
									layer.msg('删除成功！', {icon: 6});
								}
							});
						}, function(){
					  	}
					  );
				} else {
					layer.alert('请选择一项要删除的记录！', {icon: 8});
				}
			},
			goBack:function () { 
				parent.layer.close(parent.layer.getFrameIndex(window.name));
			},
			setAdmin:function(data){
				var rows = this.table.checkStatus('roleGroupDatagrid').data;
				var ids = "";
				if (rows.length == 1) {
					rows[0].isAdmin = data;
					$.ajax({
						url : staticURL+'/basic/roleGroupAction/edit',
						data : JSON.stringify(rows[0]),
						contentType:"application/json",
						success : function(response) {
							app.query();
							layer.msg('成功！', {icon: 6});
						}
					});
				} else {
					layer.alert('请选择一项记录！', {icon: 8});
				}
			}
		},
		created: function () {
			this.init();
			window._refresh = this.query;
		},
		updated:function(){
		}
	});
});